"use strict";

const crypto = require("crypto");

// Configure workload via CLI arg or env var if desired
const iterations = Number(process.argv[2] || process.env.ITERATIONS || 200000);

function runExpensiveTask() {
	// CPU-intensive but deterministic workload
	crypto.pbkdf2Sync("password", "fixed_salt", iterations, 64, "sha512");
}

function formatTimestamp(date) {
	const pad = (n) => String(n).padStart(2, "0");
	return (
		`${date.getFullYear()}-` +
		`${pad(date.getMonth() + 1)}-` +
		`${pad(date.getDate())} ` +
		`${pad(date.getHours())}:` +
		`${pad(date.getMinutes())}:` +
		`${pad(date.getSeconds())}.` +
		`${String(date.getMilliseconds()).padStart(3, "0")}`
	);
}

const startWallClock = new Date();
const start = process.hrtime.bigint();
runExpensiveTask();
const end = process.hrtime.bigint();
const elapsedMs = Number(end - start) / 1e6;

console.log(`[${formatTimestamp(startWallClock)}] iterations=%d`, iterations);
console.log("Sampled elapsed: %d ms", elapsedMs.toFixed(2));


